<?php
namespace RMD\AccountBundle\Form\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Collections\ArrayCollection;

class TestTransformer implements DataTransformerInterface
{

    private $om;

    public function __construct(ObjectManager $om)
    {
        $this->om = $om;
    }
	public function transform($value) {
		/*if (null === $value) {
			return array('');
			//return '';
		}
		/*$res = array();
		foreach ($groupsCollection as $group) {
			$res[] = $group->getTitle();
		}
		return $res;	*/
		$choices = array();
		/*foreach ($value as $group) {
			$choices[$group->getId()] = $group->getTitle();
		}*/
		/*foreach ($value as $group) {
			$choices[] = $group->getId();
		}*/
		//return $choices;
		return $value;
	}
	
	public function reverseTransform($value) {
		if (null === $value) {
			return null;
		}
		$res =  new ArrayCollection();
		if (is_array($value)) {
			foreach ($value as $id) {
				$group = $this->om->getRepository('RMDAccountBundle:Group')->find($id);
				$res[] = $group;
			}
		} else {
			$group = $this->om->getRepository('RMDAccountBundle:Group')->find($value);
			$res[] = $group;			
		}
		return $res;
	}
}
